﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace ManagoMes.Read.API.Applications.ProductionRead
{
    public class ProductionSearchHandler : IRequestHandler<ProductionSearchCommand, CodePageDTO<ProductionDTO>>
    {
        private readonly IBateRepository<ProductionDTO> _productionRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productionRepository"></param>
        public ProductionSearchHandler(IBateRepository<ProductionDTO> productionRepository)
        {
            _productionRepository = productionRepository;
        }
        /// <summary>
        /// 生产报工列表+分页+条件查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<CodePageDTO<ProductionDTO>> Handle(ProductionSearchCommand request, CancellationToken cancellationToken)
        {
            //连接数据库
            DapperHelper dapper = new DapperHelper();
            string conn = dapper.MangoMesRead;
            //分页查询
            CodePageDTO<ProductionDTO> str = new CodePageDTO<ProductionDTO>();
            var pageNum = (request.pageIndex - 1)*request.pageSize;
            //实体DTO
            ProductionDTO pro = new ProductionDTO();
            //sql语句
            string sql = "select * from productionwork inner join productionworkorders " +
            "on productionwork.ProductionWorkOrdersID=productionworkorders.ProductionWorkOrdersID " +
            "inner join picking on productionworkorders.ProductionWorkOrdersNo=picking.ProductionWorkOrdersNo " +
            "inner join productmanage on productionworkorders.ProductsNo=productmanage.productManageNode " +
            "inner join department on productionwork.departmentId=department.departmentId " +
            "inner join productiontype on productionwork.ProductionTypeId=productiontype.ProductionTypeId " +
            "where productionwork.ProductionWorkState=0";
            var result = _productionRepository.GetAll(sql, pro, conn).ToList();
            if (request.ProductionTypeId > -1)
            {
                result = result.Where(p => p.ProductionTypeId == request.ProductionTypeId).ToList();
            }
            if (!string.IsNullOrEmpty(request.WorkStationName))
            {
                result = result.Where(p => p.WorkStationName.Contains(request.WorkStationName)).ToList();
            }
            if (!string.IsNullOrEmpty(request.ProductionWorkOrdersNo))
            {
                result = result.Where(p=>p.ProductionWorkOrdersNo == request.ProductionWorkOrdersNo).ToList();
            }
            if (!string.IsNullOrEmpty(request.ProductManageNode))
            {
                result = result.Where(p => p.ProductManageNode == request.ProductManageNode).ToList();
            }
            if (!string.IsNullOrEmpty(request.ProductManageName))
            {
                result = result.Where(p => p.ProductManageName.Contains(request.ProductManageName)).ToList();
            }
            if (!string.IsNullOrEmpty(request.DepartmentName))
            {
                result = result.Where(p => p.DepartmentName.Contains(request.DepartmentName)).ToList();
            }
            if(request.ProductionStart > -1)
            {
                result = result.Where(p => p.ProductionStart == Convert.ToString(request.ProductionStart)).ToList();
            }
            str.TotalCount = result.Count();
            str.PageCount = (int)Math.Ceiling((result.Count() * 1.0) / request.pageSize);
            result = result.OrderBy(p=>p.ProductionWorkId).Skip((request.pageIndex-1)*request.pageSize).Take(request.pageSize).ToList();
            str.Data = result;
            foreach (var item in str.Data)
            {
                item.ProductionWorkTimes = item.ProductionWorkTime.ToString("yyyy-MM-dd");
                item.ProductionStart = item.ProductionStart == "0"?"草稿" : (item.ProductionStart == "1" ? "已确认":
                    (item.ProductionStart == "2" ? "审批中": (item.ProductionStart == "3" ? "已审批": "已完成")));
            }
            str.Code = 0;
            return str;
        }
    }
}
